package Stream流;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class Test {
    public static void main(String[] args) {
        //创建一个List集合，存储姓名
        List<String> list = new ArrayList<>();
        list.add("张无忌");
        list.add("张有忌");
        list.add("张忌");
        list.add("李无忌");

        //使用普通方法处理筛选
//        ordinary(list);

        //延迟方法:返回值类型仍然是Stream接口自身类型的方法，因此支持链式调用
//        stream(list);

//        filter();

//        map();

//        limit();

//        skip();

//        cc();

        //终结方法:返回值类型不再是Stream接口自身类型的方法，因此不再支持链式调用
//        foreach();

        count();
    }

    private static void count() {
        //count方法用来统计流中元素的个数
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        Stream<Integer> stream = list.stream();
        long count = stream.count();
        System.out.println(count);
    }

    private static void foreach() {
        //forEach方法用来遍历流中的数据
        Stream<String> stream = Stream.of("张三", "李四", "王五");
        stream.forEach(name -> System.out.println(name));
    }

    private static void cc() {
        //concat方法用于把流组合到一起
        Stream<String> stream1 = Stream.of("哈哈","嘻嘻","啵啵");
        String[] arr = {"每洋洋","xiyangyang"};
        Stream<String> stream2 = Stream.of(arr);
        Stream<String> concat = Stream.concat(stream1,stream2);
        concat.forEach(name-> System.out.println(name));
    }

    private static void skip() {
        //skip方法用于跳过元素
        String[] arr = {"哈哈","嘻嘻","啵啵"};
        Stream<String> stream = Stream.of(arr);
        Stream<String> stream2 = stream.skip(3);
        stream2.forEach(name-> System.out.println(name));
    }

    private static void limit() {
        //limit方法用来对流进行截取
        String[] arr = {"哈哈","嘻嘻","啵啵"};
        Stream<String> stream = Stream.of(arr);
        Stream<String> stream2 = stream.limit(1);
        stream2.forEach(name-> System.out.println(name));
    }

    private static void map() {
        //map方法用来将流中的元素映射到另一个流中
        Stream<String> stream = Stream.of("1","2","3");
        Stream<Integer> stream2 = stream.map((String s)->Integer.parseInt(s));
        stream2.forEach(i-> System.out.println(i));
    }

    private static void filter() {
        //forEach方法用来对数据进行过滤
        Stream<String> stream = Stream.of("张三","李四","王五");
        Stream<String> stream2 = stream.filter((String name)-> name.startsWith("张"));
        stream2.forEach(name-> System.out.println(name));
    }

    private static void stream(List<String> list) {
        list.stream()
                .filter(name -> name.startsWith("张"))
                .filter(name -> name.length()==3)
                .forEach(name -> System.out.println(name));
    }

    private static void ordinary(List<String> list) {
        //对List集合中的元素进行过滤，只要以张开头的元素，就存储到一个新的集合中
        List<String> listA = new ArrayList<>();
        for (String s : list) {
            if(s.startsWith("张")){
                listA.add(s);
            }
        }

        //对listA集合进行过滤，只要姓名长度为3的人，存储到一个新的集合中
        List<String> listB = new ArrayList<>();
        for (String s : listA){
            if(s.length()==3){
                listB.add(s);
            }
        }

        //遍历listB集合
        for(String s : listB) {
            System.out.println(s);
        }
    }
}
